Zepto(function ($) {
  wx.config({
    debug: false,
    appId: appId,
    timestamp: timestamp,
    nonceStr: nonceStr,
    signature: signature,
    jsApiList: ['scanQRCode', 'chooseWXPay']
  });

  var tid = null;
  var startb = document.querySelector('#start');
  var stopb = document.querySelector('#stop');
  var messaged = document.querySelector('#message');

  startCharging = function () {
    //alert('开始充电');
    $.ajax({
      type: 'GET',
      url: '/api/rechargerequest.do?tid=' + tid,
      timeout: 60000,
      success: function (data) {
        console.log('start data', data);
        if (data.code === 1) {
          startb.classList.add('hidden');
          messaged.textContent = '充电中...';
          var tick = function () {
              startb.classList.add('hidden');
              messaged.textContent = '充电中...';
              stopb.classList.remove('hidden');
            };
          setTimeout(tick, 3000);
          //alert('充电中');
        } else if (data.code === 2) {
          messaged.textContent = '电桩已经被占用请重试';
        } else if(data.code ===  3){
        	messaged.textContent = '电桩未连枪';
        }else if(data.code ===  4){
        	messaged.textContent = '电桩故障';
        }else if(data.code ===  5){
        	messaged.textContent = '电桩未注册';
        }else {
          messaged.textContent = '出错! 请重新扫码';
        }
      },
      error: function (xhr, type) {
        messaged.textContent = '出错！ 请重试';
        console.log('error: ' + JSON.parse(xhr) + '\n' + 'type: ' + type);
      }
    });
  };

  stopCharging = function () {
    var messagediv = document.querySelector('#message');
    stopb.classList.add('weui_btn_disabled');
    messagediv.textContent = '等待账单中';
    //
    var timeout;
    var tn = 0;
    var tick = function () {
      tn = tn + 1;
      if (tn % 6 === 0) {
        messagediv.textContent = '等待账单中';
      } else {
        messagediv.textContent = messagediv.textContent + '.';
      }
      timeout = setTimeout(tick, 1000);
    };
    tick();
    //
    //alert('停止充电');
    $.ajax({
      type: 'GET',
      url: '/api/chargestop.do?tid=' + tid,
      timeout: 60000,
      success: function (data) {
        clearTimeout(timeout);
        console.log('stop data:', data);
        if (data.code === 1) {
          console.log('stop 1');
          stopb.classList.add('hidden');
          messaged.textContent = '获取账单成功, 请支付';
          if (data.code === 1) {
            wx.chooseWXPay({
              timestamp: data.wxreturn.timeStamp,
              nonceStr: data.wxreturn.nonceStr,
              package: data.wxreturn.package,
              signType: data.wxreturn.signType,
              paySign: data.wxreturn.paySign,
              success: function (res) {
                messaged.textContent = '支付成功 ';
              }
            });
          } else {
            messaged.textContent = '账单生成失败! 请重试';
            stopb.classList.remove('hidden');
          }
        } else {
          messaged.textContent = '出错！ 请重试';
          console.log('stop 2');
        }
      },
      error: function (xhr, type) {
        messaged.textContent = '出错！ 请重试';
        console.log('error: ' + JSON.parse(xhr) + '\n' + 'type: ' + type);
      }
    });
  }

  wx.ready(function () {
    wx.scanQRCode({
      needResult: 1,
      desc: 'scanQRCode desc',
      success: function (res) {
        console.log(res);
        var tempNum = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
        var param = {
          url: url,
          openid: openid,
          appid: appId,
          pileid: tempNum
          //pileid: '1234567812345678'
        };

        //alert('param' + JSON.stringify(param));

        startb.onclick = startCharging;
        stopb.onclick = stopCharging;

        $.ajax({
          type: 'POST',
          url: '/api/chargerequest.do',
          data: JSON.stringify(param),
          dataType: 'json',
          timeout: 60000,
          success: function (data) {
            console.log('data:', data);
            if (data.code === 1) {
              tid = data.re;
              messaged.textContent = '扫码成功';
              startb.classList.remove('hidden');
            } else if (data.code === 2) {
                messaged.textContent = '电桩已经被占用请重试';
            } else if(data.code ===  3){
            	messaged.textContent = '电桩未连枪';
            }else if(data.code ===  4){
            	messaged.textContent = '电桩故障';
            }else if(data.code ===  5){
            	messaged.textContent = '电桩未注册';
            }else if(data.code ===  6){
            	wx.chooseWXPay({
                    timestamp: data.wxreturn.timeStamp,
                    nonceStr: data.wxreturn.nonceStr,
                    package: data.wxreturn.package,
                    signType: data.wxreturn.signType,
                    paySign: data.wxreturn.paySign,
                    success: function (res) {
                      messaged.textContent = '支付成功 ';
                    }
                  });
            }else {
              messaged.textContent = '出错! 请重新扫码';
            }
          },
          error: function (xhr, type) {
            messaged.textContent = '出错！ 请重试';
            console.log('error: ' + JSON.parse(xhr) + '\n' + 'type: ' + type);
          }
        });
      } 
    });
  });
});
